package pro.cherish.smartfile.rest.download;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ZeroCopyHttpOutputMessage;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pro.cherish.smartfile.common.exception.BusinessException;
import pro.cherish.smartfile.component.FileManager;
import pro.cherish.smartfile.module.entity.Attachment;
import pro.cherish.smartfile.service.AttachmentService;
import pro.cherish.smartfile.util.ZipUtils;
import reactor.core.publisher.Mono;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author 创建人：lys
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2020-12-01 lys 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：IndexController
 * @Description 功能说明：
 * <p>
 *
 * </p>
 * **********************************************************************
 * @date 创建日期： 2020-12-01 lys
 */

@RestController
@RequestMapping("/api/v1/file")
@Slf4j
@Api(value = "文件上传/下载")
public class FileDownLoadController {

    @Autowired
    private AttachmentService attachmentService;

    @Autowired
    private FileManager fileManager;

    @GetMapping("/download")
    @ApiOperation("文件下载")
    public Mono<Void> downLoadFile(ServerHttpResponse response, @ApiParam(value = "文件MD5") String id) throws IOException {
        Attachment attachment = attachmentService.queryFile(id);
        String originName = attachment.getSubmittedFileName();
        ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) response;
        response.getHeaders().set(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(originName, "UTF-8"));
        response.getHeaders().setContentType(MediaType.APPLICATION_OCTET_STREAM);
        File zipArchive = zipArchive(attachment.getFileMd5());
        if (ObjectUtil.isNull(zipArchive)) {
            throw new BusinessException("文件[{}]压缩失败", attachment.getFileMd5());
        }
        return zeroCopyResponse.writeWith(zipArchive, 0, zipArchive.length());
    }

    /**
     * @param identify 文件压缩
     * @Description:
     * @Param:
     * @return: java.io.File
     * @Author: lys
     * @Date: 2020/12/10
     */
    public File zipArchive(String identify) {
        String archiveFile = fileManager.getArchiveFile(identify);
        if (StrUtil.isNotBlank(archiveFile)) {
            try {
                String archiveZip = new File(archiveFile).getParent() + File.separator + identify + "_归档.zip";
                ZipUtils.zip(archiveFile, archiveZip);
                return new File(archiveZip);
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }
}
